package com.x.uikit.multistateview;

import androidx.annotation.NonNull;

import com.x.uikit.link.XParams;
import com.x.uikit.page.WeakListeners;

public class MultiStater {
    public enum State {INIT, EMPTY, ERROR, LOADING, SUCCESS}

    protected WeakListeners<MultiStateListener> mListeners = new WeakListeners<>();
    protected State mState = State.INIT;

    public void update(State state, XParams params) {
        if (state == null || state == mState) {
            return;
        }
        onUpdate(state, mState, params);
        mState = state;
    }

    private void onUpdate(State newState, State oldState, XParams params) {
        mListeners.callback(new WeakListeners.ICallbackTask<MultiStateListener>() {
            @Override
            public void run(@NonNull MultiStateListener listener) {
                listener.onStateChanged(newState, oldState, params);
            }
        });
    }

    public void addListener(MultiStateListener listener) {
        mListeners.addListener(listener);
    }

    public void removeListener(MultiStateListener listener) {
        mListeners.removeListener(listener);
    }

    @NonNull
    public State getState() {
        return mState;
    }
}
